package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: TODO
 * @author:
 * @date: 2023/2/5 21:28
 * @version: v1.0
 */
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierDao supplierDao;

    /**
     * @description: 分页列表
     * @author
     * @date 2023/2/6 9:46
     */
    @Override
    public Map<String, Object> getSupplierList(Integer page, Integer rows, String supplierName) {
        HashMap<String, Object> map = new HashMap<>();

        page=page==0?1:page;
        int startPage=(page-1)*rows;

        List<Supplier> list=supplierDao.getSupplierList(startPage,rows,supplierName);
        map.put("rows", list);
        return map;
    }

    /**
     * @description: 增加和修改
     * @author
     * @date 2023/2/6 9:46
     */

    @Override
    public int save(Supplier supplier) {
        int result =0;
       if(supplier.getSupplierId()==null){
           //新增
           result=supplierDao.insert(supplier);
       }else{
           //修改
           result=supplierDao.update(supplier);

       }

        return result;
    }

    /**
     * @description: 删除的接口
     * @author
     * @date 2023/2/10 8:30
     */
    @Override
    public void delete(String ids) {
        //批量删除(将前端传过来的字符串转换为数组)
        String[] idArray = ids.split(",");

        //循环id数组，达到批量删除
        for (String id : idArray) {
            supplierDao.delete(id);
        }
    }
}
